extends "res://BaseEnemy.gd"

# 精英冰龙特有属性
@export var frost_breath_cooldown = 8.0  # 冰霜吐息冷却时间
@export var frost_breath_damage = 20  # 冰霜吐息伤害
@export var frost_breath_width = 60.0  # 冰霜吐息宽度
@export var frost_breath_length = 200.0  # 冰霜吐息长度
@export var wing_slash_cooldown = 6.0  # 翼击冷却时间
@export var wing_slash_damage = 25  # 翼击伤害
@export var wing_slash_radius = 120.0  # 翼击半径
@export var ice_spike_cooldown = 12.0  # 冰刺冷却时间
@export var ice_spike_damage = 15  # 冰刺伤害
@export var ice_spike_count = 8  # 冰刺数量

var frost_breath_timer = 0.0
var wing_slash_timer = 0.0
var ice_spike_timer = 0.0
var can_frost_breath = true
var can_wing_slash = true
var can_ice_spike = true

func _ready():
	# 调用父类的_ready方法
	super._ready()
	
	# 设置精英冰龙特有属性
	max_health = 250
	current_health = max_health
	movement_speed = 90.0
	damage = 22
	experience_value = 50
	is_elite = true
	
	# 更新生命值条
	if health_bar:
		health_bar.max_value = max_health
		health_bar.value = current_health
	
	# 设置外观
	if sprite:
		sprite.modulate = Color(0.6, 0.8, 1.3)  # 精英冰龙颜色

func _physics_process(delta):
	# 调用父类的_physics_process方法
	super._physics_process(delta)
	
	# 处理冰霜吐息冷却
	if not can_frost_breath:
		frost_breath_timer += delta
		if frost_breath_timer >= frost_breath_cooldown:
			can_frost_breath = true
			frost_breath_timer = 0.0
	
	# 处理翼击冷却
	if not can_wing_slash:
		wing_slash_timer += delta
		if wing_slash_timer >= wing_slash_cooldown:
			can_wing_slash = true
			wing_slash_timer = 0.0
	
	# 处理冰刺冷却
	if not can_ice_spike:
		ice_spike_timer += delta
		if ice_spike_timer >= ice_spike_cooldown:
			can_ice_spike = true
			ice_spike_timer = 0.0
	
	# 如果可以使用冰霜吐息且玩家存在，尝试使用冰霜吐息
	if can_frost_breath and target_player and is_instance_valid(target_player):
		var distance = global_position.distance_to(target_player.global_position)
		if distance < 180 and distance > 50 and randf() < 0.02:  # 每帧2%几率
			frost_breath_attack()
	
	# 如果可以使用翼击且玩家存在，尝试使用翼击
	if can_wing_slash and target_player and is_instance_valid(target_player):
		var distance = global_position.distance_to(target_player.global_position)
		if distance < 100 and randf() < 0.03:  # 每帧3%几率
			wing_slash_attack()
	
	# 如果可以使用冰刺且玩家存在，尝试使用冰刺
	if can_ice_spike and target_player and is_instance_valid(target_player):
		var distance = global_position.distance_to(target_player.global_position)
		if distance < 150 and randf() < 0.01:  # 每帧1%几率
			ice_spike_attack()

func frost_breath_attack():
	if animation_player and animation_player.has_animation("frost_breath"):
		animation_player.play("frost_breath")
	
	# 创建冰霜吐息效果
	var frost_breath = preload("res://FrostBreathEffect.tscn").instantiate()
	get_tree().get_root().add_child(frost_breath)
	
	# 设置冰霜吐息属性
	var direction = Vector2.RIGHT
	if target_player and is_instance_valid(target_player):
		direction = (target_player.global_position - global_position).normalized()
	
	frost_breath.global_position = global_position
	frost_breath.direction = direction
	frost_breath.damage = frost_breath_damage
	frost_breath.width = frost_breath_width
	frost_breath.length = frost_breath_length
	frost_breath.source = self
	
	# 重置冰霜吐息冷却
	can_frost_breath = false
	frost_breath_timer = 0.0
	
	print("精英冰龙使用冰霜吐息!")

func wing_slash_attack():
	if animation_player and animation_player.has_animation("wing_slash"):
		animation_player.play("wing_slash")
	
	# 创建翼击效果
	var wing_slash = preload("res://WingSlashEffect.tscn").instantiate()
	get_tree().get_root().add_child(wing_slash)
	wing_slash.global_position = global_position
	wing_slash.damage = wing_slash_damage
	wing_slash.radius = wing_slash_radius
	wing_slash.source = self
	
	# 检测范围内的玩家
	if target_player and is_instance_valid(target_player):
		var distance = global_position.distance_to(target_player.global_position)
		if distance <= wing_slash_radius:
			# 对玩家造成伤害
			target_player.take_damage(wing_slash_damage)
			
			# 击退效果
			var knockback_direction = (target_player.global_position - global_position).normalized()
			if target_player.has_method("apply_knockback"):
				target_player.apply_knockback(knockback_direction * 200)
	
	# 重置翼击冷却
	can_wing_slash = false
	wing_slash_timer = 0.0
	
	print("精英冰龙使用翼击!")

func ice_spike_attack():
	if animation_player and animation_player.has_animation("ice_spike"):
		animation_player.play("ice_spike")
	
	# 等待动画中点
	await get_tree().create_timer(0.5).timeout
	
	# 创建冰刺
	for i in range(ice_spike_count):
		var angle = TAU * i / ice_spike_count
		var offset = Vector2(cos(angle), sin(angle)) * 50
		
		var ice_spike = preload("res://IceSpikeGroundEffect.tscn").instantiate()
		get_tree().get_root().add_child(ice_spike)
		ice_spike.global_position = global_position + offset
		ice_spike.damage = ice_spike_damage
		ice_spike.source = self
		
		# 延迟出现
		ice_spike.scale = Vector2.ZERO
		var tween = create_tween()
		tween.tween_property(ice_spike, "scale", Vector2(1, 1), 0.2)
	
	# 重置冰刺冷却
	can_ice_spike = false
	ice_spike_timer = 0.0
	
	print("精英冰龙使用冰刺!")

# 重写攻击方法
func attack_player():
	# 随机选择一种攻击
	var attack_type = randi() % 3
	
	match attack_type:
		0:
			if can_frost_breath:
				frost_breath_attack()
				return
		1:
			if can_wing_slash:
				wing_slash_attack()
				return
		2:
			if can_ice_spike:
				ice_spike_attack()
				return
	
	# 如果选择的攻击不可用，使用普通攻击
	super.attack_player()
